package com.fizzkai.p2p.core.service.impl;

import com.fizzkai.p2p.core.domain.UserInfo;
import com.fizzkai.p2p.core.mapper.UserInfoMapper;
import com.fizzkai.p2p.core.service.IUserInfoService;
import com.fizzkai.p2p.core.util.AssertUtil;
import com.fizzkai.p2p.core.util.BitStateUtil;
import com.fizzkai.p2p.core.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int save(UserInfo entity) {
        return userInfoMapper.insert(entity);
    }

    @Override
    public int update(UserInfo enetity) {
        int row = userInfoMapper.updateByPrimaryKey(enetity);
        AssertUtil.updateNotZero(row);
        return row;
    }

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public UserInfo getCurrent() {
        return userInfoMapper.selectByPrimaryKey(UserContext.getCurrentLoginInfo().getId());
    }

    @Override
    public void basicInfoSave(UserInfo temp) {
        // 获取 当前 userInfo
        Long id = UserContext.getCurrentLoginInfo().getId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        // 新值 设置进 旧值
        userInfo.setEducationBackground(temp.getEducationBackground());
        userInfo.setHouseCondition(temp.getHouseCondition());
        userInfo.setIncomeGrade(temp.getIncomeGrade());
        userInfo.setMarriage(temp.getMarriage());
        userInfo.setKidCount(temp.getKidCount());
        // 设置状态
        if (!userInfo.isHasBasicInfo()) {
            userInfo.addState(BitStateUtil.HAS_BASICINFO);
        }
        // 更新数据库
        update(userInfo);
    }
}
